package yzy.ecps.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import yzy.ecps.model.ShopCart;
import yzy.ecps.service.CartService;

@Controller
@RequestMapping("/cart")
public class CartController {
	@Autowired
	private CartService cartService;
	
	@RequestMapping("/validCookie.do")
	public void validCookie(HttpServletRequest request,
			HttpServletResponse response,PrintWriter out) throws IOException{
		
		String result = cartService.validCookie(request, response);
		out.write(result);
	}
	
	@RequestMapping("/validStock.do")
	public void validStock(HttpServletRequest request,
			HttpServletResponse response,PrintWriter out,Long skuId,Integer quantity)throws Exception{
		String result = cartService.validStock(request, skuId, quantity);
		out.write(result);
	}
	
	@RequestMapping("/addCart.do")
	public void addCart(HttpServletRequest request,
			HttpServletResponse response,Long skuId,Integer quantity,PrintWriter out)throws Exception{
		cartService.addCart(request, response, skuId, quantity);
		out.write("success");
	}
	
	@RequestMapping("listCart.do")
	public String listCart(HttpServletRequest request,Model model) throws Exception{
		List<ShopCart> cartList = cartService.getCartInfo(request);
		model.addAttribute("cartList", cartList);
		return "/shop/car";
	}
	
	@RequestMapping("/deleteCart.do")
	public String deleteCart(HttpServletRequest request,
			HttpServletResponse response,PrintWriter out,Long skuId) throws Exception{
		cartService.deleteCart(request, response, skuId);
		return "redirect:listCart.do";
	}
	
	
	@RequestMapping("/clearCart.do")
	public String clearCart(HttpServletRequest request,
			HttpServletResponse response)throws Exception{
		cartService.clearCart(request, response);
		return "redirect:listCart.do";
	}
}
